import { getUserList } from "../../api/user"
import { OSS_URL } from "../../contanst/url";
const OAM = ['push', 'pop', 'shift', 'unshift', 'short', 'reverse', 'splice']
import { handleCurrentChange, handleSizeChange } from "../../page/page"
class UserController {
  name = "user";
  methods = {
    handleCurrentChange,
    handleSizeChange,
    getList: (component) => {
      getUserList(this.currentData.listQuery, component);
    },

    handleResetSearch: () => {
      this.currentData.listQuery = {
        pageNum: 1,
        username: undefined,
        sex: undefined,
        mobilePhone: undefined
      }
    }
  };

  types = {
    obj: '[object Object]',
    array: '[object Array]'
  }

  constructor() {
    this.currentData = {
      list: [],
      total: 0,
      sex: [
        "女",
        "男"
      ],
      radioCheckIndex: 0,
      checked: false,
      checkedValue: 0,
      ossURL: OSS_URL,
      listLoading: false,
      listQuery: {
        pageNum: 1,
        username: undefined,
        sex: undefined,
        mobilePhone: undefined
      }
    };

    this._callback = (value) => {
      console.log(value);
    }

    // this.observe(this.currentData);
  };

  set(key, value) {
    this.currentData[key] = value;
  }

  getCurrentData() {
    return this.currentData;
  }
  created(component) {
    this.methods.getList(component);
  }
  observe(obj, path) {
    if (Object.prototype.toString.call(obj) === this.types.array) {
      this.overrideArrayProto(obj, path);
    }
    Object.keys(obj).forEach((key) => {
      let oldVal = obj[key];
      let pathArray = path && path.slice();
      if (pathArray) {
        pathArray.push(key);
      } else {
        pathArray = [key];
      }
      Object.defineProperty(obj, key, {
        get: function () {
          return oldVal;
        },
        set: (function (newVal) {
          if (oldVal !== newVal) {
            if (Object.prototype.toString.call(newVal) === '[object Object]') {
              this.observe(newVal, pathArray);
            }
            this._callback(newVal, oldVal, pathArray)
            oldVal = newVal
          }
        }
        ).bind(this)
      })
      if (Object.prototype.toString.call(obj[key]) === this.types.obj
        || Object.prototype.toString.call(obj[key]) === this.types.array) {
        this.observe(obj[key], pathArray)
      }
    }, this)
  }
  overrideArrayProto(array, path) {
    // 保存原始 Array 原型  
    var originalProto = Array.prototype, // 通过 Object.create 方法创建一个对象，该对象的原型是Array.prototype  
      overrideProto = Object.create(Array.prototype), self = this, result;
    // 遍历要重写的数组方法  
    OAM.forEach((method) => {
      Object.defineProperty(overrideProto, method, {
        value: function () {
          var oldVal = this.slice();
          //调用原始原型上的方法  
          result = originalProto[method].apply(this, arguments);
          //继续监听新数组  
          self.observe(this, path);
          self._callback(this, oldVal, path);
          return result;
        }
      })
    }
    );
    // 最后 让该数组实例的 __proto__ 属性指向 假的原型 overrideProto  
    array.__proto__ = overrideProto;

  }

}

export { UserController }